package app.search.client;

import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.GroupStartOpen;
import com.smartgwt.client.types.ListGridFieldType;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.layout.Layout;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;

import com.smartgwt.client.widgets.HTMLFlow;
import com.smartgwt.client.widgets.form.fields.LinkItem;

import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.events.DrawEvent;
import com.smartgwt.client.widgets.events.DrawHandler;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.HTMLPane;

import com.smartgwt.client.util.SC;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.widgets.tab.TabSet;
import com.smartgwt.client.widgets.tab.Tab;
import com.smartgwt.client.widgets.calendar.Calendar;

import com.google.gwt.user.client.Window;

import java.lang.StringBuffer;

import app.search.client.ClassDataSource;

public class GridExpansionComponent extends VLayout {

  private TabSet tabSet = null;

  public GridExpansionComponent(final Record record, final TabSet tabSet) {
      this.tabSet = tabSet;
      this.setAlign(Alignment.CENTER);
      this.setPadding(5);
      HTMLFlow htmlFlow = new HTMLFlow();
      final String className = record.getAttributeAsString("name");
      StringBuffer sb = new StringBuffer();
      sb.append("<br> Address: " 
         + record.getAttributeAsString("address") 
         + " "
         + record.getAttributeAsString("city") 
         + ", "
         + record.getAttributeAsString("state") 
         + " "
         + record.getAttributeAsString("zip") 
         + "</br>");
      sb.append("<br> Phone: " + record.getAttributeAsString("phone") + "</br>");
      htmlFlow.setContents(sb.toString());
      HLayout detailLayout = new HLayout(10);
      detailLayout.setAlign(Alignment.CENTER);
      //LinkItem detailLink = new LinkItem();
      //detailLink.setTitle("");
      //detailLink.setLinkTitle("details");
      /*
      detailLink.addClickHandler(new ClickHandler() {
        public void onClick(ClickEvent event) {
          SC.say("showing details...");
        }
      });
      */
      IButton detailButton = new IButton("details");
      detailButton.addClickHandler(new ClickHandler() {
          public void onClick(ClickEvent event) {
	    if(tabSet.getTabNumber(className)==-1) {
              Tab tab = new Tab();
              tab.setTitle(className+"&nbsp;&nbsp;");
              tab.setID(className);
              tab.setIcon("pieces/16/cube_green.png");
              tab.setCanClose(true);
              Canvas calendarCanvas = new VLayout(10);
              calendarCanvas.addChild(new Calendar());
              tab.setPane(calendarCanvas);
              tabSet.addTab(tab);
              tabSet.selectTab(className);
	    } else {
              tabSet.selectTab(className);
	    }
          }
      });
      IButton registerButton = new IButton("register");
      registerButton.addClickHandler(new ClickHandler() {
        public void onClick(ClickEvent event) {
          //SC.say("going to registration/signup page...");
          String key = "akey";
          Window.open("http://az.youngchampionsofamerica.com?key="+key, "_self", "");
        }
      });
      String facebookLikeBox = "<iframe src=\"http://www.facebook.com/plugins/livefeed.php?app_id=114447358569254&amp;width=400&amp;height=500\" scrolling=\"no\" frameborder=\"0\" style=\"border:none; overflow:hidden; width:400px; height:500px;\" allowTransparency=\"true\"></iframe>";
      String facebookLike = "<iframe src=\"http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.youngchampionsofamerica.org&amp;layout=button_count&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light&amp;height=21\" scrolling=\"no\" frameborder=\"0\" style=\"border:none; overflow:hidden; width:450px; height:21px;\" allowTransparency=\"true\"></iframe>";
      HTMLPane shareHTML = new HTMLPane();
      shareHTML.setWidth(230);
      shareHTML.setHeight(50);
      shareHTML.setShowEdges(false);
      shareHTML.setScrollbarSize(0);
      shareHTML.setContents(facebookLike);
      //IButton likeButton = new IButton("like");
      //likeButton.addClickHandler(new ClickHandler() {
      //  public void onClick(ClickEvent event) {
      //    SC.say("a billion young champions like developing skills for life and support YCOA (on Facebook)...");
      //  }
      //});
      detailLayout.addMember(detailButton);
      detailLayout.addMember(registerButton);
      //detailLayout.addMember(likeButton);
      this.addMember(htmlFlow);
      this.addMember(detailLayout);
      this.addMember(shareHTML);
  }

}
